﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EncuentraWEB
{
    public partial class Formulario_web15 : System.Web.UI.Page
    {
        private ArqJsCls arq;
        private clsRecorridos obj;
        protected void Page_Load(object sender, EventArgs e)
        {
            var sUser = (string)Session["User"];
            if (sUser != "admin")
            {
                Response.Redirect("~\\default.aspx");
                return;
            }
            arq = new ArqJsCls();
            obj = new clsRecorridos();
            if (!this.IsPostBack)
            {
                try
                {
                    var objLinea = new clsLineas();
                    DropLinea.DataTextField = "varDenominacion";
                    DropLinea.DataValueField = "intIdLinea";
                    var dtLineas = objLinea.buscar(0, "");
                    var drLinea = dtLineas.NewRow();
                    drLinea["intIdLinea"] = 0;
                    drLinea["varDenominacion"] = "TODOS";
                    dtLineas.Rows.Add(drLinea);
                    DropLinea.DataSource = dtLineas;
                    DropLinea.DataBind();
                    DropLinea.SelectedIndex = DropLinea.Items.Count - 1;
                    Buscar();
                    hidIndice.Value = "-1";
                }
                catch (Exception ex)
                {
                }
            }

        }

        protected void btnImgFiltrar_Click(object sender, ImageClickEventArgs e)
        {
            Buscar();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void btnImgEditar_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
           Ver();
        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void grdEmpresas_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void grdEmpresas_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }
        #region Metodos Privados
        private void Buscar()
        {
            try
            {
            DataTable dt = obj.buscar(0, Convert.ToInt32(DropLinea.SelectedValue));
            int b = dt.Rows.Count;
            int a = b %= 10;
            if (a != 0)
            {
                for (int i = 1; i <= (10 - a); i++)
                {
                    dt.Rows.Add();
                }
            }

            
                this.grdEmpresas.DataSource = dt;
                this.grdEmpresas.DataBind();
            }
            catch (Exception ex)
            {
            }
        }
        private void Ver()
        {
            if (Convert.ToInt32(hidIndice.Value) >= 0)
            {
                var id = grdEmpresas.Rows[Convert.ToInt32(hidIndice.Value)].Cells[1].Text;
                Response.Redirect("frmPuntosRecorridos.aspx?Linea="+ DropLinea.SelectedValue.ToString() + "&Recorrido=" + id );     
            }
            else
            {
                arq.Msg(this, "No se ha seleccionado ninguna fila");
            }
        }
        #endregion
    }
}
